import winston from "winston"
import dayjs from "dayjs";
import fetch from "node-fetch"
import proxy from "node-global-proxy"
// const proxy = require("node-global-proxy").default;
import {HttpsProxyAgent} from "https-proxy-agent"

// proxy.default.setConfig({
// 	http: "http://localhost:80",
// 	https: "https://localhost:80",
// });
// proxy.default.start();
const logger = winston.createLogger({
	level: 'notice', format: winston.format.json(), transports: [
		new winston.transports.File({filename: 'product-core.ruleRpcService.canBuyDays.log'})
	]
});
const logger2 = winston.createLogger({
	level: 'notice', format: winston.format.json(), transports: [
		new winston.transports.File({filename: 'product.api.ticket.tips.get.log'})
	]
});
// 使用日志
// logger.info('这是一条信息级别的日志');
// logger.error('这是一条错误级别的日志');

const header = {
	"Content-Type": "application/json",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" + "Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090b19)XWEB/11097",

	"access-token": "eyJhbGciOiJIUzUxMiJ9.eyJ1IjoiNzcxNzE4MTk4Njk3NDcyMDAwIiwidCI6IjAiLCJleHAiOjE3MjMzNDIxODd9.lRYqqMAfKgEegL00eydHn9kRglpVNCUhTtfEc5I6tJSIp9adV68YmAIyW21P4CHMEpT6L-19zJD5yZlUFCSZqw",
	"app": "app_qqmap_tickets",
	"mpDeviceToken":
	"v2:wqkBxuGpmmC9gsbW3LkihEY/4aYks2d7+qEE4W0W0pEVwIoxpb995KZfYKON5Sz2MaZv6/rcb8kRuM+3n8GR94MavmonuY3R6K5nDgUbcGzp6LguaEtfNFeqONiRsrKjpz/9CS8JEAYZGHNm2DiVGea1Qjd3Y/m7EZbz0RNPQYFye3DqJeAGcl0pUIFyYakaanWxXRYwcuuh/EkwPU+Cv+R6JXap5tWQUDZ8Jrd7jhMLL/w=",
	"Referer": "https://servicewechat.com/wx13169e68a3e63e55/140/page-frame.html",
}


const getData = ({url, params}) => {

	return fetch(url, {
		"agent":new HttpsProxyAgent("http://10.10.120.1:8888"),

		headers: header, ...params,
	}).then(async (x) => {
		const r = await x.json()
		return r
	})

}
const delayTime = () => {
	return new Promise((resolve, reject) => {

		setTimeout(() => {

			resolve(true)
		}, 1000 + Math.random() * 3000)
	})

}
const circleGetCanBuyDays = async () => {

	const r = await getData({url: "https://lotswap.dpm.org.cn/dubboApi/product-core/ruleRpcService/canBuyDays"})
	const msg = dayjs().format("YYYY-MM-DD HH:mm:ss") + '   data:' + r.data;
	logger.info(msg)
	console.log(msg)
	console.log('-'.repeat(150))
	// await delayTime()
	//
	// await circleGetCanBuyDays()
}

const circleGetCanBuyDays2 = async () => {

	const r = await getData({
		url: "https://lotswap.dpm.org.cn/lotsapi/product/api/ticket/tips/get",
		params: {
			method: "POST",
			body:JSON.stringify({"merchantInfoId":"22655"})
		}
	})
	console.log(r)
	const msg = dayjs().format("YYYY-MM-DD HH:mm:ss") + '   data:' + JSON.stringify(r.data);
	logger2.info(msg)
	console.log(msg)
	console.log('-'.repeat(150))


}
(async ()=>{

	while (true){

		await delayTime()
		await circleGetCanBuyDays();
		await delayTime()
		await circleGetCanBuyDays2();

	}
})()

// circleGetCanBuyDays();
// circleGetCanBuyDays2();